package com.zbj.eureka.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * ConsumerApplication
 * 这里 @EnableDiscoveryClient 和 @EnableEurekaClient 可以互换，EnableDiscoveryClient 应用范围可以使Eureka或者其他注册中心(Consul、zk、Eureka)，EnableEurekaClient 只能用于Eureka
 * 官方建议使用 EnableDiscoveryClient
 * 参见文章 https://blog.csdn.net/lizhiqiang1217/article/details/89883519
 *
 * @author weigang
 * @create 2019-07-30
 **/
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RequestMapping("/consumer")
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    /**
     * 此处方法 hello() 想访问 zbj-provider 对应服务 则需要开启客户端负载均衡(@LoadBalanced)
     * nested exception is java.net.UnknownHostException: zbj-provider
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @GetMapping("/hello/{hello}")
    public String hello(@PathVariable String hello) {
        return restTemplate().getForObject("http://zbj-provider/provider/hello/" + hello, String.class);
    }
}